<template>
  <div class="captcha" v-html="svg" @click="refresh"></div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import { getCaptcha } from '@/api/login'

export interface CaptchaInst {
  refresh: () => Promise<void>
}

defineProps<{
  value: string
}>()

const emit = defineEmits<{
  'update:value': [value: string]
  change: []
}>()

/** svg */
const svg = ref('')

async function refresh() {
  const result = await getCaptcha()

  svg.value = result.data

  emit('update:value', result.captchaId)
  emit('change')
}

onMounted(() => {
  refresh()
})

defineExpose<CaptchaInst>({
  refresh
})
</script>

<style lang="scss" scoped>
.captcha {
  margin-left: 10px;
  min-width: 100px;
  width: 100px;
  height: 40px;
  cursor: pointer;
  background-color: #f9f9f9;

  :deep(svg) {
    display: block;
    width: 100%;
    height: 100%;
  }
}
</style>
